/*
This file was produced by Deep Exploration Plugin: CPP Export filter. 

Copyright (C) 1999-2006 Right Hemisphere
Mail support@righthemisphere.com for support.
Visit http://www.righthemisphere.com/dexp.htm for updates.
*/
#include <windows.h>
#include <GL\gl.h>
#include <GL\glu.h>


struct sample_MATERIAL
{
	GLfloat ambient[3];
	GLfloat diffuse[3];
	GLfloat specular[3];
	GLfloat emission[3];
	GLfloat alpha;
	GLfloat phExp;
	int texture;
};

static sample_MATERIAL materials [1] = {
 {{0.117647f,0.117647f,0.117647f},	{0.752941f,0.752941f,0.752941f},	{0.301177f,0.301177f,0.301177f},	{0.0f,0.0f,0.0f},	1.0f,8.0f,-1} //Explorer Default
};

// 8 Verticies
// 4 Texture Coordinates
// 6 Normals
// 12 Triangles

static BYTE face_indicies[12][9] = {
// Cube
	{3,1,0 ,0,0,0 ,0,1,2 }, {2,3,0 ,0,0,0 ,3,0,2 }, {5,3,2 ,1,1,1 ,0,1,2 },
	{4,5,2 ,1,1,1 ,3,0,2 }, {7,5,4 ,2,2,2 ,0,1,2 }, {6,7,4 ,2,2,2 ,3,0,2 },
	{1,7,6 ,3,3,3 ,0,1,2 }, {0,1,6 ,3,3,3 ,3,0,2 }, {5,7,1 ,4,4,4 ,0,1,2 },
	{3,5,1 ,4,4,4 ,3,0,2 }, {2,0,6 ,5,5,5 ,0,1,2 }, {4,2,6 ,5,5,5 ,3,0,2 }
};
static GLfloat vertices [8][3] = {
{-0.104f,-0.5f,-0.02f},{-0.104f,-0.5f,0.02f},{0.104f,-0.5f,-0.02f},
{0.104f,-0.5f,0.02f},{0.104f,0.5f,-0.02f},{0.104f,0.5f,0.02f},
{-0.104f,0.5f,-0.02f},{-0.104f,0.5f,0.02f}
};
static GLfloat normals [6][3] = {
{0.0f,-1.0f,0.0f},{1.0f,0.0f,0.0f},{0.0f,1.0f,0.0f},
{-1.0f,0.0f,0.0f},{0.0f,0.0f,1.0f},{0.0f,0.0f,-1.0f}
};
static GLfloat textures [4][2] = {
{1.0f,1.0f},{0.0f,1.0f},{0.0f,0.0f},
{1.0f,0.0f}
};
/*Material indicies*/
/*{material index,face count}*/
static int material_ref [1][2] = {
{0,12}
};
void MyMaterial(GLenum mode, GLfloat * f, GLfloat alpha)
{
	GLfloat d[4];
	d[0] = f[0];
	d[1] = f[1];
	d[2] = f[2];
	d[3] = alpha;
	glMaterialfv(GL_FRONT_AND_BACK, mode, d);
}

/*
 *  SelectMaterial uses OpenGL commands to define facet colors.
 *
 *  Returns:
 *    Nothing
 */

void SelectMaterial(int i)
{
//
// Define the reflective properties of the 3D Object faces.
//
	glEnd();
	GLfloat alpha = materials[i].alpha;
	MyMaterial(GL_AMBIENT, materials[i].ambient, alpha);
	MyMaterial(GL_DIFFUSE, materials[i].diffuse, alpha);
	MyMaterial(GL_SPECULAR, materials[i].specular, alpha);
	MyMaterial(GL_EMISSION, materials[i].emission, alpha);
	glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, materials[i].phExp);
	glBegin(GL_TRIANGLES);

};

GLint Gen3DObjectList()
{
	int i;
	int j;

 GLint lid=glGenLists(1);
	int mcount=0;
	int mindex=0;
   glNewList(lid, GL_COMPILE);

    glBegin (GL_TRIANGLES);
      for(i=0;i<sizeof(face_indicies)/sizeof(face_indicies[0]);i++)
       {
	if (!mcount)
	{
		SelectMaterial(material_ref[mindex][0]);
		mcount = material_ref[mindex][1];
		mindex ++;
	}
	mcount --;
       for(j=0;j<3;j++)
        {
          int vi=face_indicies[i][j];
          int ni=face_indicies[i][j+3];//Normal index
          int ti=face_indicies[i][j+6];//Texture index
           glNormal3f (normals[ni][0],normals[ni][1],normals[ni][2]);
           glTexCoord2f(textures[ti][0],textures[ti][1]);
           glVertex3f (vertices[vi][0],vertices[vi][1],vertices[vi][2]);
        }
       }
    glEnd ();

   glEndList();
   return lid;
};
